package org.example.trigger.http;


import lombok.extern.slf4j.Slf4j;
import org.example.api.response.Response;
import org.example.domain.order.model.entity.OrderEntity;
import org.example.domain.order.service.IOrderService;
import org.example.domain.transaction.model.entity.PayCallbackEntity;
import org.example.domain.transaction.model.entity.RefundCallBackEntity;
import org.example.domain.transaction.service.ITransactionService;
import org.example.types.enums.ResponseCode;
import org.example.types.exception.AppException;
import org.example.types.utils.ThreadLocalUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author yinghuashen
 * @version 1.0
 * @description:
 * @date 2025/3/15 21:26
 */
@Slf4j
@RestController
@CrossOrigin("*")
@RequestMapping("/test")
public class TestController {

    @Resource
    private IOrderService orderService;

    @Resource
    private ITransactionService transactionService;

    @RequestMapping("/lock_order")
    public Response<String> lockOrder(@RequestParam String userId,
                                      @RequestParam String userName,
                                      @RequestParam String userPhone,
                                      @RequestBody OrderEntity orderEntity) {
        ThreadLocalUtils.addCurrentUser(userId, userName, userPhone);
        orderEntity.setUserId(userId);
        String orderId = null;
        try {
            orderId = orderService.lockOrder_v2(orderEntity);
            if(orderId == null){
                return Response.<String>builder()
                       .code(ResponseCode.ORDER_LOCK_ERROR.getCode())
                       .info(ResponseCode.ORDER_LOCK_ERROR.getInfo())
                       .data(null).build();
            }
            return Response.<String>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(orderId).build();
        } catch (Exception e) {
            return Response.<String>builder()
                    .code(ResponseCode.ORDER_LOCK_ERROR.getCode())
                    .info(ResponseCode.ORDER_LOCK_ERROR.getInfo())
                    .data(null).build();
        } finally {
            ThreadLocalUtils.removeUser();
        }
    }

    @RequestMapping("/pay_callback")
    public Response<String> payCallback(@RequestParam String userId,
                                      @RequestParam String userName,
                                      @RequestParam String userPhone,
                                      @RequestBody PayCallbackEntity payCallbackEntity) {
        ThreadLocalUtils.addCurrentUser(userId, userName, userPhone);
        try {
            transactionService.payCallBack(payCallbackEntity);
            return Response.<String>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(null).build();
        } catch (AppException e) {
            return Response.<String>builder()
                    .code(e.getCode())
                    .info(e.getInfo())
                    .data(null).build();
        } finally {
            ThreadLocalUtils.removeUser();
        }
    }

    @RequestMapping("/refund_callback")
    public Response<String> refundCallback(@RequestParam String userId,
                                        @RequestParam String userName,
                                        @RequestParam String userPhone,
                                        @RequestBody RefundCallBackEntity refundCallBackEntity) {
        ThreadLocalUtils.addCurrentUser(userId, userName, userPhone);
        try {
            transactionService.refundCallBack(refundCallBackEntity);
            return Response.<String>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(null).build();
        } catch (AppException e) {
            return Response.<String>builder()
                    .code(e.getCode())
                    .info(e.getInfo())
                    .data(null).build();
        } finally {
            ThreadLocalUtils.removeUser();
        }
    }

}
